using System.Collections;
using UnityEngine;

public class PlayerAreaBehavior : MonoBehaviour
{
	public EntityBehavior nearCar;

	private GameObject objPlayer;

	private PlayerBehavior scriptPlayer;

	private PhotonView photonView;

	private ButtonCar buttonCar;

	private IEnumerator _checkCars;

	private void Init()
	{
		objPlayer = base.gameObject;
		scriptPlayer = objPlayer.GetComponent<PlayerBehavior>();
		photonView = objPlayer.GetComponent<PhotonView>();
		buttonCar = GameTopWindow.instance.buttonGetInCar.GetComponent<ButtonCar>();
	}

	private void OnEnable()
	{
		if (objPlayer == null)
		{
			Init();
		}
		if (_checkCars == null && (settings.offlineMode || photonView.isMine))
		{
			_checkCars = UpdateNearCar();
			StartCoroutine(_checkCars);
		}
	}

	private void OnDisable()
	{
		if (_checkCars != null)
		{
			StopCoroutine(_checkCars);
			_checkCars = null;
		}
	}

	private IEnumerator UpdateNearCar()
	{
		float second = 0.01f;
		while (true)
		{
			nearCar = null;
			if (GameController.instance != null && GameController.instance.arrAllCar != null && scriptPlayer != null)
			{
				float minDistance2 = float.MaxValue;
				foreach (EntityBehavior curCar in GameController.instance.arrAllCar)
				{
					if (!(curCar == null) && (!(curCar is TitanBehavior) || GameController.instance.curTypeGame != TypeGame.WalkingRobots) && (!curCar.privateCar || !(PrivateCarManager.instance != null) || !(PrivateCarManager.instance.currentCar != curCar)) && !curCar.isDead)
					{
						float dist = Vector3.Distance(curCar.transform.position, objPlayer.transform.position);
						if (dist < curCar.distanceFindCar && dist < minDistance2)
						{
							minDistance2 = dist;
							nearCar = curCar;
						}
					}
				}
				minDistance2 = float.MaxValue;
				CheckButton();
			}
			yield return new WaitForSeconds(second);
			second = 1f;
		}
	}

	private void CheckButton()
	{
		if ((!settings.offlineMode && !photonView.isMine) || !(GameController.instance.playerScript != null))
		{
			return;
		}
		bool flag = false;
		bool flag2 = false;
		if (GameController.instance != null && GameController.instance.curTypeGame == TypeGame.Zombe && PlayerBehavior.MyPlayer.localTeam == PunTeams.Team.red)
		{
			flag = false;
			flag2 = false;
		}
		else if (!GameController.instance.playerScript.onLadder && !GameController.instance.playerScript.isShock && !GameController.instance.playerScript.isDead)
		{
			if (scriptPlayer.carIWantGet == null && nearCar != null && nearCar.isFree)
			{
				buttonCar.SetCarType(nearCar.Type);
				RaycastHit hitInfo;
				if (Physics.Linecast(GameController.instance.playerScript.transform.position + Vector3.up, nearCar.transform.position + Vector3.up, out hitInfo) && MessageWindow.instance != null && !MessageWindow.instance.showButtonCar)
				{
					EntityBehavior componentInParent = hitInfo.collider.GetComponentInParent<EntityBehavior>();
					if (componentInParent != null && componentInParent == nearCar)
					{
						flag = nearCar.isFree;
					}
				}
				else
				{
					flag = false;
					flag2 = false;
				}
			}
			else
			{
				flag = GameController.instance.playerScript.inCar && !TutorialManager.TuturialWorking();
				flag2 = GameController.instance.playerScript.inHelic && !TutorialManager.TuturialWorking();
			}
		}
		if (GameTopWindow.instance != null && GameTopWindow.instance.buttonGetInCar.activeSelf != (flag || flag2))
		{
			buttonCar.gameObject.SetActive(flag || flag2);
		}
	}
}
